package com.epalmpay.controller.agent.datareport;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.datareport.MemberDataReportInDTO;
import com.epalmpay.dto.datareport.MemberDataReportListOutDTO;
import com.epalmpay.entity.StatisAgent;
import com.epalmpay.entity.StatisAgentDay;
import com.epalmpay.entity.StatisAgentMonth;
import com.epalmpay.entity.StatisShopDay;
import com.epalmpay.entity.StatisShopMonth;
import com.epalmpay.service.datareport.IMemberDataReportService;

/** 
 * 说明：数据报表 :代理商会员数据报表
 * 创建人：gwn
 * 创建时间：2017-06-20
 */
@Controller
@RequestMapping(value="/agent/memberDataReport")
public class AgentMemberDataReportController extends BaseController{


	@Resource
	private IMemberDataReportService memberDataReportService;
	
	
	@RequestMapping(value="/list")
	@ResponseBody
	public MemberDataReportListOutDTO List(MemberDataReportInDTO memberDataReportInDTO){
		
		MemberDataReportListOutDTO memberDataReportListOutDTO= new MemberDataReportListOutDTO();
		
		StatisAgentDay statisAgentDay=null;
		StatisShopDay statisShopDay=null;
		StatisShopMonth statisShopMonth=null;
		StatisAgentMonth statisAgentMonth=null;
		
		try {
			
			if (memberDataReportInDTO.getQueryDayOrMonth()==1) {
				
				if (memberDataReportInDTO.getJoinStart()==null) {
//					memberDataReportInDTO.setJoinStart(new Date());
					Date date=new Date();//取时间
					Calendar calendar = new GregorianCalendar();
					calendar.setTime(date);
					calendar.add(calendar.DATE,-1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
					date=calendar.getTime();
					memberDataReportInDTO.setJoinStart(date);
				}
				
			    statisAgentDay = memberDataReportService.queryStatisAgentDayCountAgent(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisAgentDay(statisAgentDay);
				
				statisShopDay = memberDataReportService.queryStatisShopDayCountShop(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisShopDay(statisShopDay);
				
			}else{
				
			    statisAgentMonth = memberDataReportService.queryStatisAgentMonthCountAgent(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisAgentMonth(statisAgentMonth);
				
				statisShopMonth = memberDataReportService.queryStatisShopMonthCountShop(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisShopMonth(statisShopMonth);
			}

			StatisAgent statisAgent = new StatisAgent();
			int totalCount = memberDataReportService.queryMemberCountAgent();
			statisAgent.setTotalCount(totalCount);
			memberDataReportListOutDTO.setStatisAgent(statisAgent);
			if (statisAgentDay!=null && statisShopDay!=null) {
				
				statisAgent.setConcernMemberNum(statisAgentDay.getConcernMemberNum()+statisShopDay.getConcernMemberNum());
				statisAgent.setPerfectMemberNum(statisAgentDay.getPerfectMemberNum()+statisShopDay.getPerfectMemberNum());
				statisAgent.setUnconcernMemberNum(statisAgentDay.getUnconcernMemberNum()+statisShopDay.getUnconcernMemberNum());
			}
			if (statisAgentMonth!=null && statisShopMonth!=null) {
				statisAgent.setConcernMemberNum(statisAgentMonth.getConcernMemberNum()+statisShopMonth.getConcernMemberNum());
				statisAgent.setPerfectMemberNum(statisAgentMonth.getPerfectMemberNum()+statisShopMonth.getPerfectMemberNum());
				statisAgent.setUnconcernMemberNum(statisAgentMonth.getUnconcernMemberNum()+statisShopMonth.getUnconcernMemberNum());
			}
			
			memberDataReportListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberDataReportListOutDTO;
		} catch (ApplicationException ae) { 
			memberDataReportListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			memberDataReportListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
		}
		return memberDataReportListOutDTO;
		
	}
	

}


